package com.augurit.wf.mgmt.convert;

import java.util.ArrayList;
import java.util.List;

import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.history.HistoryActivityInstance;
import org.jbpm.jpdl.internal.model.JpdlProcessDefinition;
import org.jbpm.pvm.internal.history.model.HistoryActivityInstanceImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;

import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.sysmeta.web.form.SysMetaTableForm;
import com.augurit.wf.mgmt.web.form.WfActivityInstanceForm;
import com.augurit.wf.mgmt.web.form.WfProcessDefForm;
import com.augurit.wf.mgmt.web.form.WfProcessInstanceForm;
import com.augurit.wf.mgmt.web.form.WfProcessVersionForm;

public class ConvertToExtModel {
	/**
	 * 把流程定义列表转换为
	 * @param list
	 * @return
	 */
	public static List<WfProcessDefForm> convertProcDefListToSimpleFormList(List<ProcessDefinition> list){
		List<WfProcessDefForm> results = null;
		if(list != null && list.size() > 0){
			results = new ArrayList<WfProcessDefForm>();
			
			for(ProcessDefinition jpdlProcessDef:list){
				
				JpdlProcessDefinition curProcessDef = (JpdlProcessDefinition)jpdlProcessDef;
				WfProcessDefForm curform = new WfProcessDefForm();
				curform.setId(curProcessDef.getId());
				curform.setName(curProcessDef.getName());
				curform.setKey(curProcessDef.getKey());
				curform.setDescription(curProcessDef.getChineseName());
				results.add(curform);
			}
		}
		return results;
	} 
	
	
	public static List<WfProcessVersionForm> convertProcVerListToFormList(List<ProcessDefinition> list){
		List<WfProcessVersionForm> results = null;
		if(list != null && list.size() > 0){
			results = new ArrayList<WfProcessVersionForm>();
			
			for(ProcessDefinition processDef:list){
				WfProcessVersionForm form = new WfProcessVersionForm();
				String version = WfProcessVersionForm.formatVersion(processDef.getVersion());
				form.setVersion(version);
				results.add(form);
			}
		}
		return results;
	} 
	
	public static List<BasicCombo> convertToVersionSelectBox(List<ProcessDefinition> list){
		
		List<BasicCombo> nodes = null;
		if(list != null && list.size() > 0){
			nodes = new ArrayList<BasicCombo>();
			
			nodes.add(BasicCombo.getBlankItem());
			for(ProcessDefinition form:list){
				BasicCombo basicCombo = new BasicCombo();
				basicCombo.setLabel(String.valueOf(form.getVersion()));
				basicCombo.setValue(String.valueOf(form.getVersion()));
				nodes.add(basicCombo);
			}
		}
		
		return nodes;
	}
	
	public static List<BasicCombo> convertToMetaTableSelectBox(List<SysMetaTableForm> list){
		
		List<BasicCombo> nodes = null;
		if(list != null && list.size() > 0){
			nodes = new ArrayList<BasicCombo>();
			
			nodes.add(BasicCombo.getBlankItem());
			for(SysMetaTableForm form:list){
				BasicCombo basicCombo = new BasicCombo();
				basicCombo.setLabel(form.getTableName());
				basicCombo.setValue(form.getEntityName());
				nodes.add(basicCombo);
			}
		}
		
		return nodes;
	}
	
	public static List<WfProcessInstanceForm> convertToProcessInstanceList(List<ProcessInstance> list){
		List<WfProcessInstanceForm> result = new ArrayList<WfProcessInstanceForm>();
		if(list != null && list.size() > 0){
			for(ProcessInstance instance:list){
				ExecutionImpl executionImpl = (ExecutionImpl)instance;
				WfProcessInstanceForm instanceForm = new WfProcessInstanceForm();
				instanceForm.setId(instance.getId());
				//instanceForm.setActivityName(instance.findActiveActivityNames().iterator().next());
				instanceForm.setActivityName(executionImpl.getActivityName());
				instanceForm.setState(instance.getState());
				instanceForm.setProcessInstanceName(executionImpl.getName());
				result.add(instanceForm);
			}
		}
		return result;
	}
	
	/**
	 * 把历史活动实例封闭成WfActivityInstanceForm集合
	 * @param list 活动实例历史记录
	 * @param processInstanceId 流程实例ID
	 * @return WfActivityInstanceForm集合
	 */
	public static List<WfActivityInstanceForm> convertToActivityInstanceList(List<HistoryActivityInstance> list,String processInstanceId){
		List<WfActivityInstanceForm> result = new ArrayList<WfActivityInstanceForm>();
		if(list != null && list.size() > 0){
			for(HistoryActivityInstance historyActivityInstance:list){
				
				HistoryActivityInstanceImpl historyActivityInstanceImpl = (HistoryActivityInstanceImpl)historyActivityInstance;
				
				WfActivityInstanceForm activityInstanceForm = new WfActivityInstanceForm();
				
				activityInstanceForm.setDbid(historyActivityInstanceImpl.getDbid());
				activityInstanceForm.setExecutionId(historyActivityInstance.getExecutionId());
				activityInstanceForm.setEndTime(historyActivityInstance.getEndTime());
				activityInstanceForm.setActivityName(historyActivityInstance.getActivityName());
				activityInstanceForm.setProcInstId(processInstanceId);
				activityInstanceForm.setStartTime(historyActivityInstance.getStartTime());
				activityInstanceForm.setType(historyActivityInstance.getType());
				
				if(historyActivityInstance.getTransitionNames() != null && historyActivityInstance.getTransitionNames().size() == 1)
					activityInstanceForm.setTransitionName(historyActivityInstance.getTransitionNames().get(0));
				
				
				if(historyActivityInstance.getEndTime() != null)
					activityInstanceForm.setState("完成");
				else
					activityInstanceForm.setState("处理中");
				
				
				result.add(activityInstanceForm);
			}
		}
		return result;
	}

}
